function mode = compute_mode_fpeak(data,binsize,sensitivity,smoother,smooth_value)

%make a histogram of data
[x n]=hist(data, binsize);
r=smooth(x,smooth_value,smoother);
%fpeak function to find peaks in smoothed histogram data.  fpeak function
%has a tendancy to return peaks at the beginning and end of data, range
%variable at end prevents this
%peaks =fpeak(n,r,mode.sensitivity,[2 max(n)-1 10 Inf]);
%mode.values = peaks(:,1)';
peaks =fpeak(n,r,sensitivity,[2 max(n)-1 10 Inf]);
if(length(peaks)<1)
    mode(1).value=NaN;
else
    
    for i=1:length(peaks(:,1))
        mode(i).type='fpeak';
        mode(i).binsize=binsize;
        mode(i).sensitivity=sensitivity;
        mode(i).smoother=smoother;
        mode(i).smooth_value=smooth_value;
        mode(i).value=peaks(i,1);
    end
    if(length(mode)>1 && length(mode) < 3)
        [mode ind centroid] = compute_mode_boundaries(data,mode);
    end
end

function [mode ind centroid] = compute_mode_boundaries(data,mode)
numOfmodes=length(mode);
[ind centroid]=kmeans(log(data),numOfmodes,'dist','sqeuclidean');
for i=1:length(mode)
    mode(i).lower=min(data(ind==i));
    mode(i).upper=max(data(ind==i));
    mode(i).counts=length(data(ind==i));
    mode(i).percentage=mode(i).counts/length(data);
end